package @{ctrlPkg};


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import @{modelPkg}.@{EntityModel};
import @{daoPkg}.@{EntityModel}Dao;
import com.zzwtec.interfaces.ResultObject;

import com.zzwtec.common.util.BeanUtil;
import com.zzwtec.common.util.ErrorMsg;
import com.zzwtec.interfaces.Page;
import com.zzwtec.interfaces.RequestObject;
import @{dtoPkg}.@{EntityModel}DTO;
import @{servicePkg}.@{EntityModel}Service;
<?
var date = date();
?>


/**
 * 微服务@{EntityModel}Service接口实现类
 * @author  草原狼
 * 
 * @{date,'yyyy年MM月dd日'}
 */

@RestController
public class @{EntityModel}Controller implements @{EntityModel}Service {
	
	@Autowired
	private DiscoveryClient discoveryClient;

	@Resource(name = "@{entityModel}Dao")
	private @{EntityModel}Dao @{entityModel}Dao;

	@Override
	public ResultObject<String> add@{EntityModel}(@RequestBody RequestObject<@{EntityModel}DTO> request) {
		ResultObject<String> result = new ResultObject<String>(request);
		@{EntityModel}DTO dto = request.getData();
		@{EntityModel} bean = new @{EntityModel}();
		BeanUtil.copyProperties(bean, dto);		
		@{entityModel}Dao.insert(bean);
		result.setData(bean.getId());
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
		
	}
	
	@Override
	public ResultObject<Void> remove@{EntityModel}(@RequestBody RequestObject<String> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		@{entityModel}Dao.deleteById(request.getData());
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;	
		
	}
	
	@Override
	public ResultObject<Void> remove@{EntityModel}ByIds(RequestObject<String[]> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		@{entityModel}Dao.deleteByIds(request.getData());
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
	}
	

	@Override
	public ResultObject<Void> modify@{EntityModel}(@RequestBody RequestObject<@{EntityModel}DTO> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		@{EntityModel}DTO dto = request.getData();
		@{EntityModel} bean = new @{EntityModel}();
		BeanUtil.copyProperties(bean, dto);		
		@{entityModel}Dao.update(bean);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
		
	}


	@Override
	public ResultObject<List<@{EntityModel}DTO>> queryAll@{EntityModel}(@RequestBody RequestObject<Void> request) {
		ResultObject<List<@{EntityModel}DTO>> result = new ResultObject<List<@{EntityModel}DTO>>(request);
		List<@{EntityModel}> list = @{entityModel}Dao.selectAll();
		List<@{EntityModel}DTO> data = new ArrayList<@{EntityModel}DTO>();
		for(@{EntityModel} entity : list){
			@{EntityModel}DTO dto = new @{EntityModel}DTO();
			BeanUtil.copyProperties(dto, entity);
			data.add(dto);
		}
		result.setData(data);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;	
		
	}


	@Override
	public ResultObject<@{EntityModel}DTO> query@{EntityModel}ById(@RequestBody RequestObject<String> request) {
		ResultObject<@{EntityModel}DTO> result = new ResultObject<@{EntityModel}DTO>(request);
		@{EntityModel} entity = @{entityModel}Dao.selectOne(request.getData());
		@{EntityModel}DTO dto = new @{EntityModel}DTO();
		BeanUtil.copyProperties(dto, entity);
		result.setData(dto);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;	
		
	}

	@Override
	public ResultObject<Page<@{EntityModel}DTO>> query@{EntityModel}ByPage(@RequestBody RequestObject<Page<@{EntityModel}DTO>> request) {
		ResultObject<Page<@{EntityModel}DTO>>  result=new ResultObject<Page<@{EntityModel}DTO>>(request);
		Page<@{EntityModel}DTO> page=request.getData();
		Page<@{EntityModel}> rPage=new Page<@{EntityModel}>();
		rPage.setCondition(page.getCondition());
		rPage.setDistinct(page.isDistinct());
		rPage.setGroupByFields(page.getGroupByFields());
		rPage.setOrderField(page.getOrderField());
		rPage.setOrderType(page.getOrderType());
		rPage.setPage(page.getPage());
		rPage.setPageNum(page.getPageNum());
		rPage.setSearchFields(page.getSearchFields());
		rPage.setSearchValues(page.getSearchValues());
		rPage = @{entityModel}Dao.selectPage(rPage);
		if(rPage.getData()!=null){
			List<@{EntityModel}DTO> data = new ArrayList<@{EntityModel}DTO>(rPage.getData().size());
			for(@{EntityModel} entity : rPage.getData()){	
				@{EntityModel}DTO dto = new @{EntityModel}DTO();
				BeanUtil.copyProperties(dto, entity);
				data.add(dto);
			}
			page.setData(data);
		}
		page.setTotal(rPage.getTotal());
		page.setRows(rPage.getRows());
		result.setData(page);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
	}
	
	
	@Override
	public ServiceInstance showServiceInfo() {		
		ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
		return localServiceInstance;
	}
}
